<?php

namespace app\apiz\controller\user;

use think\Controller;
use think\Cookie;

class Base extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->UserModel = model('User.User');
        $this->RecordModel = model('User.Record');
        $this->RechargeModel = model('User.Recharge');
    }
    // 注册
    public function register()
    {
       if($this->request->isPost())
        {
            $params = $this->request->param();
            $password = $this->request->param('password',' ');
            $repassword = $this->request->param('repassword');
            if(empty($password))
            {
                $this->error('密码不能为空');
                exit;
            }
            if($repassword!=$password)
            {
                $this->error('密码和确认密码不一致');
                exit;
            }
            $salt = randstr(10);
            $password = md5(trim($password).$salt);

            $data = [
                'email' => $params['email'],
                'password' => $password,
                'salt' => $salt,
                'nickname' => $params['email'],
                'vip' => 0
            ];
            
            $result = $this->UserModel->validate('common/User/User')->save($data);

            if($result === FALSE)
            {
                $this->error($this->UserModel->getError());
                exit;
            }else
            {
                $this->success('注册成功');
                exit;
            }
        }
       
    }
    // 登录
    public function login()
    {
        if($this->request->isPost())
        {
            $email = $this->request->param('email');
            $password = $this->request->param('password');

            $User = $this->UserModel->where(['email'=>$email])->find();
            if(!$User)
            {
                $this->error('用户不存在,请重新输入');
                exit;
            }
            $salt = $User['salt'];
            $password = md5(trim($password).$salt);

            if($password!=$User['password'])
            {
                $this->error('密码错误,请重新输入');
                exit;
            }

            $data = [
                'id' => $User['id'],
                'email' => $User['email'],
                'nickname' => $User['nickname'],
                'avatar' => $User['avatar_cdn'],
                'sex' => $User['sex'],
                'province' => $User['province'],
                'city' => $User['city'],
                'district' => $User['district'],
                'createtime' => $User['createtime'],
                'content' => $User['content'],
                'point' => $User['point'],
                'vip' => $User['vip'],
                'auth' => $User['auth'],
                'cover' => $User['cover_cdn'],
                'region'=>$User['region'],
            ];
            $this->success('登录成功','/user/base/index',$data);
            exit;
        }
    }
    //判断是否登录
    public function check()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid');
            $User = $this->UserModel->find($userid);
            if($User)
            {
                $data = [
                    'id' => $User['id'],
                    'email' => $User['email'],
                    'nickname' => $User['nickname'],
                    'avatar' => $User['avatar_cdn'],
                    'sex' => $User['sex'],
                    'province' => $User['province'],
                    'city' => $User['city'],
                    'district' => $User['district'],
                    'createtime' => $User['createtime'],
                    'content' => $User['content'],
                    'point' => $User['point'],
                    'vip' => $User['vip'],
                    'auth' => $User['auth'],
                    'cover' => $User['cover_cdn'],
                    'region'=>$User['region'],
                ];
                $this->success('登录成功',null,$data);
                exit;
            }else
            {
                $this->error('用户不存在');
                exit;

            }
        }
    }
    //邮箱验证
    public function email()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid',0);
            $User = $this->UserModel->find($userid);
            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }
            if($User['auth'] == 1)
            {
                $this->error('您已认证，无需重复认证');
                exit;
            }
            if(empty($User['email']))
            {
                $this->error('邮箱为空');
                exit;
            }
            $email = md5($User['email']);
            //组装信息
            $msg = "<a href='http://www.ask.com/home/index/email?email=$email'>点击免费邮箱验证</a>";
            
            //调用公共函数库里面邮箱发送方法
            $result = send($User['email'], $msg);

            //返回的结果
            $success = [
                'result'=>false,
                'msg'=>''
            ];

            if($result)
            {
                $success['result'] = true;
                $success['msg'] = '发送邮件成功，请注意查收';
            }else
            {
                $success['result'] = false;
                $success['msg'] = '发送邮件失败，请重新发送';
            }
            return json($success);

        }
    }
    //基本资料
    public function profile()
    {
        if($this->request->isPost())
        {
            $params = $this->request->param();
            $userid = $this->request->param('id',0);
            $password = $this->request->param('password','');

            $User = $this->UserModel->with(["provinces","citys","districts"])->find($userid);
            
            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }
            $data = [
                'id' => $userid,
                'email' => $params['email'],
                'nickname' => $params['nickname'],
                'sex' => $params['sex'],
                'province' => $params['province'],
                'city' => $params['city'],
                'district' => $params['district'],
                'content' => $params['content'],   
            ];

            if(!empty($password))
            {
                $salt = $User['salt'];
                $repassword = md5(trim($password).$salt);

                if($repassword == $User['password'])
                {
                    $this->error('密码与旧密码一致，请重新输入!');
                    exit;
                }

                //生成新密码盐
                $newsalt = randstr(10);

                $password = md5(trim($password).$newsalt);

                $data['salt'] = $newsalt;
                $data['password'] = $password;
            }

            $result = $this->UserModel->isUpdate(true)->save($data);

            if($result == FALSE)
            {
                $this->error('修改个人资料失败');
                exit;
            }
            $this->success('修改个人资料成功','/user/base/index',$data);
            exit;
        }
    }
    //图片
    public function profile_avatar()
    {
        if($this->request->isPost())
        {
            $params = $this->request->param();
            $userid = $this->request->param('id');

            $User = $this->UserModel->find($userid);

            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }

            $data = [
                'id'=>$userid,
                'nickname'=>$params['nickname'],
                'email'=>$params['email'],
                'sex'=>$params['sex'],
                'content'=>$params['content'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
            ];
            // 判断头像
            if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0 )
            {
                $result = upload('avatar');
                
                if($result === FALSE)
                {
                    $this->error('修改头像失败');
                    exit;
                }

                //追加头像
                $data['avatar'] = 'uploads/'.$result;
               
            }

            // 判断封面
            if(isset($_FILES['cover']) && $_FILES['cover']['size'] > 0 )
            {
                $result = upload('cover');
                
                if($result === FALSE)
                {
                    $this->error('修改封面背景失败');
                    exit;
                }

                //追加头像
                $data['cover'] = 'uploads/'.$result;
            }

            $result = $this->UserModel->isUpdate(true)->save($data);

            if($result == FALSE)
            {
                @is_file($data['avatar']) && @unlink($data['avatar']);
                @is_file($data['cover']) && @unlink($data['cover']);
                $this->error('修改资料失败');
                exit;
            }else
            {
                //先判断是否有选择新的图片
                if(@is_file($data['avatar']))
                {
                    //修改成功后，将原来的图片删除
                    $avatar = ltrim($User['avatar'],"/");
                    @is_file($avatar) && @unlink($avatar);
                }

                //先判断是否有选择新的图片
                if(@is_file($data['cover']))
                {
                    //修改成功后，将原来的图片删除
                    $cover = ltrim($User['cover'],"/");
                    @is_file($cover) && @unlink($cover);
                }

                $Userdata = $this->UserModel->find($userid);

                $data = [
                    'id'=>$userid,
                    'nickname'=>$Userdata['nickname'],
                    'email'=>$Userdata['email'],
                    'sex'=>$Userdata['sex'],
                    'content'=>$Userdata['content'],
                    'province'=>$Userdata['province'],
                    'city'=>$Userdata['city'],
                    'district'=>$Userdata['district'],
                    'avatar'=>$Userdata['avatar_cdn'],
                    'cover'=>$Userdata['cover_cdn'],
                    'region'=>$Userdata['region'],
                ];

                $this->success('修改资料成功','/user/base/profile',$data);
                exit;
            }

           


        }
        
    }

    public function recorddata()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $user = $this->UserModel->find($userid);
            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }
            $where = [
                'userid' => $userid
            ];
            $record = $this->RecordModel->where($where)->select();

            if($record == FALSE)
            {
                $this->error('暂无消费记录');
                exit;
            }else
            {
                $this->success('返回消费记录',null,$record);
                exit;
            }


        }
    }

    public function recorddel()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $recordid = $this->request->param('recordid', 0);

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            $record = $this->RecordModel->find($recordid);

            if(!$record)
            {
                $this->error('消费记录不存在');
                exit;
            }

            $where = [
                'id' => $recordid,
                'userid' => $userid
            ];

            $result = $this->RecordModel->where($where)->delete();

            if($result == FALSE)
            {
                $this->error('删除消费记录失败');
                exit;
            }else
            {
                $this->success('删除消费记录成功');
                exit;
            }
        }
    }

    public function recharge()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $params = $this->request->param();

            $user = $this->UserModel->find($userid);
            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }
            $data = [
                'userid' => $userid,
                'account' => $params['account'],
                'price' => $params['price']
            ];
            $result = $this->RechargeModel->validate('common/User/Recharge')->save($data);

            if($result == FALSE)
            {
                $this->error($this->RechargeModel->geterror());
                exit;
            }
            $price = $user['point']+$params['price'];
            $userdata = [
                'id' => $userid,
                'point' => $price,
            ];
            $users = $this->UserModel->isUpdate(true)->save($userdata);

            if($users == FALSE)
            {
                $this->error('充值失败，请稍后再试');
                exit;
            }else
            {
                $this->success('充值成功');
                exit;
            }



        }
    }

    public function walletdata()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $user = $this->UserModel->find($userid);
            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }else
            {
                $this->success('返回用户数据', null, $user);
                exit;
            }
        }
    }


    public function recordsdata()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $user = $this->UserModel->find($userid);
            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }
            $where = [
                'userid' => $userid
            ];
            $recharge = $this->RechargeModel->where($where)->select();

            if($recharge == FALSE)
            {
                $this->error('暂无消费记录');
                exit;
            }else
            {
                $this->success('返回消费记录',null,$recharge);
                exit;
            }


        }
    }

    public function recordsdel()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $recordsid = $this->request->param('recordsid', 0);

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            $recharge = $this->RechargeModel->find($recordsid);

            if(!$recharge)
            {
                $this->error('充值记录不存在');
                exit;
            }

            $where = [
                'id' => $recordsid,
                'userid' => $userid
            ];

            $result = $this->RechargeModel->where($where)->delete();

            if($result == FALSE)
            {
                $this->error('删除充值记录失败');
                exit;
            }else
            {
                $this->success('删除充值记录成功');
                exit;
            }
        }
    }

}
